package com.twu.biblioteca.ui.menus.books;

import com.twu.biblioteca.items.Book;
import com.twu.biblioteca.library.Library;
import com.twu.biblioteca.ui.menus.AbstractMenu;
import com.twu.biblioteca.ui.menus.items.ReturnToMainMenuItem;
import com.twu.biblioteca.ui.menus.items.books.CheckoutBookMenuItem;

import java.util.Collection;

/**
 * @author Alvi Jonathan Sait
 */
public class ListBooksMenu extends AbstractMenu {

    public ListBooksMenu(Library library) {
        super(library);
        addMenuItems();
    }

    @Override
    public String getOutput() {
        StringBuilder builder = new StringBuilder();
        Collection<Book> books = getBooks();
        builder.append(books.size()).append(" book(s) found").append(System.lineSeparator());
        for (Book book : books) {
            builder.append(book).append(System.lineSeparator());
        }
        return builder.substring(0, builder.length() - System.lineSeparator().length());
    }


    @Override
    public String getUserPrompt() {
        StringBuilder builder = new StringBuilder(generateMenuItemsOutput());
        builder.append("Please select an option:");
        return builder.toString();
    }

    protected void addMenuItems() {
        addMenuItem(new CheckoutBookMenuItem());
        addMenuItem(new ReturnToMainMenuItem());
    }

    protected Collection<Book> getBooks() {
        return getLibrary().getBooks().values();
    }

}
